/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkHttpMedia/MkHttpMediaServer.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkLog.h"
#include "MkMedia/MkFileSource.h"

class CTestHttpMediaServer : public CMkHttpMediaServer
{
public:
    CTestHttpMediaServer(CMkEventPool* pEventPool, CMkTimer* pTimer);
    ~CTestHttpMediaServer();
protected:
private:
    Uint32 OnRequestJsonParse(const MkHttpMethod& Method, const MkString& Path, const MkMap<MkString, MkString>& MapQuery, const Json::Value& JsonReq, const MkHttpResponseJsonCb& cb);
    CMkMediaSource* OnFindSource(const MkString& Path);
public:    
    MkMediaCallbacks m_Cbs;
};

CTestHttpMediaServer::CTestHttpMediaServer(CMkEventPool* pEventPool, CMkTimer* pTimer)
    :CMkHttpMediaServer(pEventPool, pTimer, [this](const MkHttpMethod& Method, const MkString& Path, const MkMapString& MapQuery, const Json::Value& JsonReq, const MkHttpResponseJsonCb& cb) {
    return NoneError;
}, [](CMkHttpSession* pSession) {}, m_Cbs)
{

}

Uint32 CTestHttpMediaServer::OnRequestJsonParse(const MkHttpMethod& Method, const MkString& Path, const MkMap<MkString, MkString>& MapQuery, const Json::Value& JsonReq, const MkHttpResponseJsonCb& cb)
{
    return NoneError;
}

CMkMediaSource* CTestHttpMediaServer::OnFindSource(const MkString& Path)
{
    MkDebugLog("find source\n");
    MkString LocalFile = "C:/Workspace/media/aa.264";
    FILE *fp = fopen(LocalFile.c_str(), "rb");
    CMkH264FileSource *pSource = new CMkH264FileSource(fp, GetTimer());
    return pSource;
}


CTestHttpMediaServer::~CTestHttpMediaServer()
{

}

int main(int argc, char ** argv)
{
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#endif
    MkLogChangeLevel(LOG_DEBUG);
    CMkEventPool Pool;
    Uint32 ErrorCode = Pool.Start();
    if (NoneError != ErrorCode) {
        MkErrorLog("start event pool failed\n");
        return 0;
    }
    CMkTimer Timer;
    CTestHttpMediaServer Svr(&Pool, &Timer);
    Svr.StartServer(58082);
    MkDebugLog("start server:%d\n", 58082);
    Timer.Dispatch();
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}